package com.ocean.aws.command;

import com.ocean.aws.aw.AWBridge;
import com.ocean.aws.config.PluginConfig;
import com.ocean.aws.util.CompressionUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public final class AWSyncCommand implements CommandExecutor {

	private final AWBridge bridge;
	private final PluginConfig config;
	private final JavaPlugin plugin;

	public AWSyncCommand(JavaPlugin plugin, AWBridge bridge, PluginConfig config) {
		this.plugin = plugin;
		this.bridge = bridge;
		this.config = config;
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (args.length == 0) {
			sender.sendMessage("/aw-sync <size|reload|status> [player]");
			return true;
		}
		String sub = args[0].toLowerCase();
		String targetName = args.length >= 2 ? args[1] : sender instanceof Player ? sender.getName() : null;
		Player target = targetName != null ? Bukkit.getPlayerExact(targetName) : null;
		switch (sub) {
			case "size":
				if (target == null) { sender.sendMessage("玩家不在线"); return true; }
				Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
					try {
						byte[] data = bridge.exportWardrobeCompressedBytes(target);
						int raw = data.length;
						int gz = config.compress ? CompressionUtils.gzip(data).length : -1;
						sender.sendMessage("Wardrobe bytes: raw=" + raw + (gz >= 0 ? (", gzip=" + gz) : ""));
					} catch (Throwable ex) {
						sender.sendMessage("size 失败: " + ex.getMessage());
					}
				});
				return true;
			case "reload":
				plugin.reloadConfig();
				sender.sendMessage("配置已重载");
				return true;
			case "status":
				sender.sendMessage("HuskSync: 启用, 压缩: " + (config.compress ? "GZIP" : "无"));
				return true;
			default:
				sender.sendMessage("用法: /aw-sync <size|reload|status> [player]");
				return true;
		}
	}
}


